package com.yuerenjf.yuerenjinfu.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.utils.CacheUtils;
import com.yuerenjf.yuerenjinfu.utils.CommonUtils;
import com.yuerenjf.yuerenjinfu.utils.ConstantUtils;
import com.yuerenjf.yuerenjinfu.widget.SFProgrssDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

//修改密码
public class RePasswordActivity extends BaseActivity {
    private EditText mRepassword_old_edit;
    private EditText mRepassword_new_edit;
    private EditText mRepassword_new_again_edit;
    private TextView mUser_tijiao_text;


    public static void launchActivity(Activity context) {
        Intent intent = new Intent(context, RePasswordActivity.class);
        context.startActivityForResult(intent,1001);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tv_title.setText("修改密码");
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    protected View getChildView() {
        View view = View.inflate(this, R.layout.activity_user_repassword, null);
        mRepassword_old_edit = (EditText) view.findViewById(R.id.repassword_old_edit);
        mRepassword_new_edit = (EditText) view.findViewById(R.id.repassword_new_edit);
        mRepassword_new_again_edit = (EditText) view.findViewById(R.id.repassword_new_again_edit);
        mUser_tijiao_text = (TextView) view.findViewById(R.id.user_tijiao_text);
        mUser_tijiao_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mRepassword_old_edit.getText().toString())) {
                    Toast.makeText(RePasswordActivity.this, "原密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!CommonUtils.isPassword(mRepassword_new_edit.getText().toString())) {
                    Toast.makeText(RePasswordActivity.this, "密码需8-20位,至少包含字母和数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mRepassword_new_again_edit.getText().toString().equals(mRepassword_new_edit.getText().toString())) {
                    Toast.makeText(RePasswordActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    return;
                }


                setRepasswordForRemote(mRepassword_old_edit.getText().toString(), mRepassword_new_again_edit.getText().toString());


            }
        });


        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    private void setRepasswordForRemote(String password, String newPwd) {


        Map<String, String> map = new HashMap<String, String>();
        String phone = CacheUtils.getString(RePasswordActivity.this, ConstantUtils.SP_USER_NAME);
        String uid = CacheUtils.getString(RePasswordActivity.this, ConstantUtils.SP_UID);
        String secret = CacheUtils.getString(RePasswordActivity.this, ConstantUtils.SP_SECRET);


        map.put("old_password", password);
        map.put("uid", uid);
        map.put("password", newPwd);
        map.put("re_password", newPwd);

        String signature = CommonUtils.md5(CommonUtils.md5(CacheUtils.getString(RePasswordActivity.this, ConstantUtils.SP_TOKEN) + password + newPwd + newPwd + uid) + secret);
        map.put("signature", signature);

        final SFProgrssDialog dialog = SFProgrssDialog.show(RePasswordActivity.this, "");
        Xutils.getInstance().post(RePasswordActivity.this, Constants.URL_MODIFY_PASSWORD, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int status = jsonObject.getInt("status");
                    String message = jsonObject.getString("message");
                    if (status == 200) {
                        Toast.makeText(RePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                        setResult(1001);
                        finish();

                        CacheUtils.delString(RePasswordActivity.this, ConstantUtils.SP_UID);
                        CacheUtils.delString(RePasswordActivity.this, ConstantUtils.SP_SECRET);
                        CacheUtils.delString(RePasswordActivity.this, ConstantUtils.SP_WEB_SIGNATURE);
                        CacheUtils.delString(RePasswordActivity.this, ConstantUtils.SP_SIGNATURE);
                        Intent intent = new Intent(RePasswordActivity.this, MainActivity.class);
                        intent.putExtra("current", "0");
                        startActivity(intent);

                    } else if (status == 300 || status == 400) {
                        Toast.makeText(RePasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(RePasswordActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailer(int status, String result) {

            }

            @Override
            public void onFinish() {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });


    }


}
